// https://leetcode.cn/problems/longest-substring-without-repeating-characters/

package main

import (
	"fmt"
)

func lengthOfLongestSubstring(s string) int {
	maxLen, startIdx, charLoc := 0, -1, make(map[rune]int)
	for i, c := range s {
		if idx, ok := charLoc[c]; ok {
			startIdx = max(startIdx, idx)
		}
		charLoc[c] = i
		maxLen = max(maxLen, i-startIdx)
	}
	return maxLen
}

func main() {
	// 3
	res := lengthOfLongestSubstring("dvdf")
	fmt.Printf("%v\n", res)
	// 2 abba
	res = lengthOfLongestSubstring("abba")
	fmt.Printf("%v\n", res)
	// 5 tmmzuxt
	res = lengthOfLongestSubstring("tmmzuxt")
	fmt.Printf("%v\n", res)
	// 2
	res = lengthOfLongestSubstring("ab")
	fmt.Printf("%v\n", res)
}
